package com.jier.bank.util;

import com.jier.bank.entity.Bank;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStreamReader;
import java.util.Iterator;

public class XMLParserUtils {
    public static final String KEY_TAG = "key";
    public static final String STRING_TAG = "string";
    public static final String INTEGER_TAG = "integer";

    public static final String BANK_NAME_REMARK = "bank_name_remark";
    public static final String BANK_CODE = "bank_code";
    public static final String BANK_NAME = "bank_name";
    public static final String AUTO_ID = "auto_id";
    public static final String CARD_NAME = "card_name";
    public static final String FYSIGN = "fysign";
    public static final String BELONG_CODE_LEN = "belong_code_len";
    public static final String NUMBER_LEN = "number_len";
    public static final String BANK_ID = "bank_id";
    public static final String BELONG_CODE = "belong_code";
    public static final String CARD_TYPE = "card_type";
    public static final String POSABLE = "posable";
    public static final String ATMABLE = "atmable";
    public static final String CARD_NUMBER = "card_number";

    /**
     * 查询银行归属信息
     *
     * @param bankNo
     * @return
     * @throws DocumentException
     */
    public static Bank parse(String bankNo) throws DocumentException {
        // 创建SAXReader对象
        SAXReader reader = new SAXReader();
        // 读取文件 转换成Document
        Document document = reader.read(new InputStreamReader(XMLParserUtils.class.getResourceAsStream("/bankList.xml")));
        // 获取array根节点元素
        Element root = document.getRootElement();
        // 获取当前节点下面的所有子节点
        Iterator<Element> iterator_array = root.elementIterator();
        Bank bank = null;
        while (iterator_array.hasNext()) {
            Element e_dict = iterator_array.next();
            // 获取当前节点下面的所有子节点
            Iterator<Element> iterator_dict = e_dict.elementIterator();
            bank = new Bank();
            while (iterator_dict.hasNext()) {
                Element e = iterator_dict.next();
                if (KEY_TAG.equals(e.getName())) {
                    if (BANK_NAME_REMARK.equals(e.getText())) {
                        bank.setBank_name_remark(iterator_dict.next().getText());
                    }
                    if (BANK_CODE.equals(e.getText())) {
                        bank.setBank_code(iterator_dict.next().getText());
                    }
                    if (BANK_NAME.equals(e.getText())) {
                        bank.setBank_name(iterator_dict.next().getText());
                    }
                    if (AUTO_ID.equals(e.getText())) {
                        bank.setAuto_id(iterator_dict.next().getText());
                    }
                    if (CARD_NAME.equals(e.getText())) {
                        bank.setCard_name(iterator_dict.next().getText());
                    }
                    if (FYSIGN.equals(e.getText())) {
                        bank.setFysign(iterator_dict.next().getText());
                    }
                    if (BELONG_CODE_LEN.equals(e.getText())) {
                        bank.setBelong_code_len(iterator_dict.next().getText());
                    }
                    if (NUMBER_LEN.equals(e.getText())) {
                        bank.setNumber_len(iterator_dict.next().getText());
                    }
                    if (BANK_ID.equals(e.getText())) {
                        bank.setBank_id(iterator_dict.next().getText());
                    }
                    if (BELONG_CODE.equals(e.getText())) {
                        bank.setBelong_code(iterator_dict.next().getText());
                    }
                    if (CARD_TYPE.equals(e.getText())) {
                        bank.setCard_type(iterator_dict.next().getText());
                    }
                    if (POSABLE.equals(e.getText())) {
                        bank.setPosable(iterator_dict.next().getText());
                    }
                    if (ATMABLE.equals(e.getText())) {
                        bank.setAtmable(iterator_dict.next().getText());
                    }
                    if (CARD_NUMBER.equals(e.getText())) {
                        bank.setCard_number(iterator_dict.next().getText());
                    }
                }
            }
            if (bank.getBelong_code().equals(bankNo)) {
                break;
            }
        }
        return bank;
    }
}
